package com.javaxiaobear.base.framework.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;

public class LongSerializer extends StdSerializer<Long> {

  public static final Long LOW = -9007199254740991L;
  public static final Long HIGH = 9007199254740991L;
  public static final LongSerializer instance = new LongSerializer();

  protected LongSerializer() {
    super(Long.class);
  }

  @Override
  public void serialize(Long value, JsonGenerator gen, SerializerProvider provider)
      throws IOException {
    if (value == null) {
      gen.writeNull();
    } else {
      if (value.compareTo(LOW) >= 0 && value.compareTo(HIGH) <= 0) {
        gen.writeNumber(value);
      } else {
        gen.writeString(value.toString());
      }
    }
  }
}
